package com.cwd.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.cwd.dao.OrderSettingDao;
import com.cwd.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService{
    @Autowired
    private OrderSettingDao orderSettingDao;
    //上传表格，批量插入数据
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //判断表格中的数据在数据库中有没有重复的
                Date orderDate = orderSetting.getOrderDate();
                long count = orderSettingDao.findCountByorderDate(orderDate);
                if (count > 0) {
                    //重复了，进行修改操作
                    orderSettingDao.update(orderSetting);
                }else {
                    //没重复，进行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据年月查询对应的预约设置信息
    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String month) {//2019-7
        String begin = month + "-1";//2019-7-1
        String end = month + "-31";//2019-7-31
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map<String, Object>> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map<String, Object> orderSettingData = new HashMap<>();
            orderSettingData.put("date",orderSetting.getOrderDate().getDate());
            orderSettingData.put("number",orderSetting.getNumber());
            orderSettingData.put("reservations",orderSetting.getReservations());
            data.add(orderSettingData);
        }
        return data;
    }

    //根据日期修改可预约人数
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        orderSettingDao.editNumberByOrderDate(orderSetting);
    }
}
